<html>
<head>
<title>权限管理</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
<link rel="shortcut icon" href="resources/images/lock.png" type="image/x-icon">
<link rel="stylesheet" href="resources/css/stylemain.css">
<link rel="stylesheet" href="resources/layui/css/layui.css">

<link rel="stylesheet" href="resources/layui/css/treeTable.css">
<link rel="stylesheet" href="resources/font-awesome-4.7.0/css/font-awesome.min.css" media="all">
<script src="resources/layui/layui.all.js"></script>
<script type="text/javascript" src="resources/js/jquery.js"></script>
<script type="text/javascript" src="resources/js/base64.js"></script>

</head>
<body>
<div class="layui-form" style="margin-top:10px">
<table id="tbl_rights" class="layui-table" lay-filter="tbl_rights"></table>
</div>
<script type="text/html" id="tbBar">
<button class="layui-btn layui-btn-sm" lay-event="edit"><i class="layui-icon layui-icon-edit"></i> 修改</button>
</script>

<script id="wrg" type="text/html">
	<form class="layui-form" action="" style="margin-top:20px;width:360px">
		<div class="layui-form-item">
		    <label class="layui-form-label">功能名称:</label>
		    <div class="layui-input-block">
		      <input type="text" id="rgtext" name="rgtext" lay-verify="required" class="layui-input">
		    </div>
		  </div>
		  <div class="layui-form-item">
	    	<label class="layui-form-label">是否复核:</label>
		    <div class="layui-input-block">
		      <div>
		        <input id="ndck" type="radio" name="needcheck" value="1" title="是" checked="">
		        <input id="ndnck" type="radio" name="needcheck" value="0" title="否">
		      </div>
		    </div>
	  	</div>
	  	<div class="layui-form-item">
	    	<label class="layui-form-label">排列顺序:</label>
		    <div class="layui-input-block">
		      <input id="rgsort" type="text" name="number" lay-verify="required|number" class="layui-input">
		    </div>
	  	</div>	  	
	</form>
</script>

<script>
var _getorg = "";
var util = layui.util;
var layer = layui.layer;
var form = layui.form;
var treetable;
var curopt = "";
var insTb;
layui.config({
    base: 'resources/layui/lay/modules/'
}).use(['treeTable'], function () {
    treetable = layui.treeTable;
    
    
    getData();
    
    function getData(){
    	var layeri = layer.load(2);
    	$.ajax({
    		type:"get",
    		contentType: "application/json;charset=UTF-8",
    		url: "righttree",
    		async:false,
         	dataType:"json",
            success: function(msg) {    //数据提交成功时返回数据
            	//debugger;
            	// 渲染表格
                insTb = treetable.render({
                    elem: '#tbl_rights',
                    data: msg,
            		height: 'full-10',
            		toolbar: '#tbBar',
            		tree: {
                        iconIndex: 1,
                        isPidData: false,
                        arrowType: 'arrow2',
                        getIcon: 'ew-tree-icon-style2',
                        onlyIconControl:true
                    },
            		cols: [
            			{type: 'radio', hide: true},
            			{
            				field: 'title',
            				title: '功能名称',	
            			},
            			{
            				field: 'needcheck',
            				title: '是否需复核',   
            				align: 'center', 
            				templet: '#statusTpl'
            			},
            			{
            				field: 'sort',
            				title: '排列顺序',
            			},
            			{
            				field:'reject',
            				title: '互斥功能',	
            				width: '100px',
            			},
            			{
            				field:'createtime',
            				title: '创建时间',				
            			},
            			{
            				field:'updatetime',
            				title: '最后更新',				
            			}
            		],
            		style: 'margin-top:0;'
                });
                layer.close(layeri);
                treetable.on('row(tbl_rights)', function(obj){
            	    var data = obj.data;    
            	    obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
            		//选中radio样式
            		obj.tr.find('i[class="layui-anim layui-icon"]').trigger("click");
            	});
            },
            error : function(e){
            	layer.close(layeri);
            	if(e.status == 200){
            		parent.location.href = "login";
            	}else{
            		alert(e.status);
                    alert(e.responseText);
            	}
            }

    	});
    }
    
    treetable.on('toolbar(tbl_rights)', function (obj) {
    	//debugger;
    	var datas = insTb.checkStatus();
    	if(datas.length == 0){
    		layer.msg("请先选择功能项!");
    		return;
    	}
        switch (obj.event) {
            case 'edit':
            	curopt = "edit";            	
            	dialog_edit(datas[0]);
                break;
        }
    });    

    function dialog_edit(data){
    	//debugger;
    	
    	var win = layer.open({
    		  type: 1 //0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层） 
    		  ,title: "编辑权限" //标题
    		  ,skin:""   //layui-layer-lan:蓝色，layui-layer-molv：墨绿色，默认灰色
    		  ,area: ['400px', '300px'] //宽度、高度
    		  ,shade: 0.3
    		  ,offset: 'auto'
    		  ,anim:5 //弹出动画
    		  ,shadeClose: false //弹框出现后，允许点击其它地方关闭
    		  ,content: $("#wrg").html()     //内容
    		  ,btn: ['确定', '取消'] //只是为了演示
    		  ,success:function(layero){
    			     var mask = $(".layui-layer-shade");
    			     mask.appendTo(layero.parent());
    			     //其中：layero是弹层的DOM对象
    			     $("#rgtext").val(data.title);
    			     if(data.needcheck == "1"){
    			    	 $("input[name=needcheck][value=0]").prop("checked","false");
    				 	 $("input[name=needcheck][value=1]").prop("checked","true");
    			     }else{
    			    	 $("input[name=needcheck][value=1]").prop("checked","false");
    				 	 $("input[name=needcheck][value=0]").prop("checked","true");
    			     }
    			 	 $("#rgsort").val(data.sort);
					 
    			 	if(data.authtype == "COMPONENT" || data.authtype == "DIR"){
    					$('#ndck').attr("disabled",true);
    					$('#ndnck').attr("disabled",true);
    				}else{
    					$('#ndck').attr("disabled",false);
    					$('#ndnck').attr("disabled",false);
    				}
    			 	form.render();
    			}
    		,yes: function(index, layero){
    		    //按钮【按钮一】的回调		    	
    		    	submitForm('','');
    			 	 
    		  }
    		  ,btn2: function(index, layero){
    		    //按钮【按钮二】的回调
    		    
    		    	//$("#useradd").css('display','none'); 
    		    	
    		  }
    		  ,cancel: function(){ 
    			    //右上角关闭回调
    			    //$("#useradd").css('display','none'); 
    			    
    		 }
    	});
    }
    
    function checkNumber(val) {
    	　　var re = /^[0-9]+.?[0-9]*$/; //判断字符串是否为数字 //判断正整数 /^[1-9]+[0-9]*]*$/ 

    	　　if (!re.test(val)) {
    	　　　　return false;
    	　　}
    	return true;
    }
    
    function submitForm(ckname,ckpassword){
    	//校验
    	//debugger;
    	if($("#rgtext").val().replace(/\s+/g,"").length < 1){
    		layer.msg("请填写正确的权限名称!");	
    		return;
    	}    	
    	
    	if( !checkNumber($('#rgsort').val())){
    		layer.msg("排列顺序必须为数字!");
    		$('#rgsort').focus();
    		return;
    	}
    	var datas = insTb.checkStatus();
    	
    	var datastring = '{"rightid":"'+datas[0].id+'","rgtext":"' + $('#rgtext').val() + '","sort":' + $('#rgsort').val()  
		+ ',"rname":"系统管理/功能管理/保存","needcheck":' + $('input[name="needcheck"]:checked').val() +',"ckname":"'+ckname +'","ckpassword":"'+ckpassword+'"}';
		$.ajax({
			type:"post",
			contentType: "application/json;charset=UTF-8",
			url: "right_edit.do?",
			async:false,
			data:datastring,
	     	dataType:"json",
	        success: function(msg) {    //数据提交成功时返回数据
	        	
	        	if(msg.status == "1"){
	        		layer.closeAll();
					getData();
	        	}else if(msg.status == "1211"){
	        		$('#ckname').val('');
					$('#ckpassword').val('');
					ckdialgo_auth();
	        	}else{
	        		layer.msg(msg.desc);	
	        	}					
	        },
	        error : function(e){
	        	if(e.status == 200){
            		parent.location.href = "login";
            	}else{
            		alert(e.status);
                    alert(e.responseText);
            	}
	        }
	
		});			
    	
    }    
    
    function ckdialgo_auth(){
    	layer.open({
    		  type: 1 //0（信息框，默认）1（页面层）2（iframe层）3（加载层）4（tips层） 
    		  ,title: "复核" //标题
    		  ,skin:"layui-layer-lan"   //layui-layer-lan:蓝色，layui-layer-molv：墨绿色，默认灰色
    		  ,area: ['350px', '260px'] //宽度、高度
    		  ,shade: 0.3
    		  ,offset: 'auto'
    		  ,anim:5 //弹出动画
    		  ,shadeClose: false //弹框出现后，允许点击其它地方关闭
    		  ,content: $("#dlgck")     //内容
    		  ,btn: ['确定', '取消'] //只是为了演示
    		  ,success:function(layero){
    			     //var mask = $(".layui-layer-shade");
    			     //mask.appendTo(layero.parent());
    			     $('#ckname').focus();
    			     //其中：layero是弹层的DOM对象
    			}
    		 ,yes: function(index, layero){
    		    //按钮【按钮一】的回调
    		    	userckopt();
    	    		$("#dlgck").css('display','none');
    			 	layer.close(index);
    		    	
    			 	 
    		  }
    		  ,btn2: function(index, layero){
    		    	//按钮【按钮二】的回调
    		    
    		    	$("#dlgck").css('display','none'); 
    		    	
    		  }
    		  ,cancel: function(){ 
    			    //右上角关闭回调
    			    $("#dlgck").css('display','none'); 
    			    
    		 }
    	});
    }
});

$('#ckpassword').on('keydown', function (event) {
    if (event.keyCode == 13) {
    	userckopt();
		layer.closeAll();
    }
});

function userckopt(){
	//debugger;
	var ckname = $('#ckname').val();
	if(ckname == ''){
		layer.msg('请填写用户账号!');
		return;
	}
	var ckpassword = $('#ckpassword').val();
	if(curopt == "reject"){
		submitForm(ckname,$.base64.encode(ckpassword));
	}else if(curopt == "edit"){
		submitForm(ckname,$.base64.encode(ckpassword));
	}
	
}

function userckopt_cancel(){
	$('#ckname').val('');
	$('#ckpassword').val('');	
}

</script>

<script type="text/html" id="statusTpl">
  {{#  if(d.needcheck == '0'){ }}	
    <span>否</span>
  {{#  } else if(d.needcheck == '1') { }}
    <span style="color: #F581B1;">是</span>
  {{#  } }}
</script>
</body>
</html>